package com.cetc.stream;

import com.cetc.lambda.Person;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * @Classname StreamTest02
 * @Description 筛选与切片测试
 * @Date 2023/1/5 21:13
 * @Created by louzhuangxi
 */
public class StreamTest02 {
    List<Person> personList = Arrays.asList(
            new Person("张三", 24),
            new Person("李四", 80),
            new Person("王五", 28),
            new Person("赵六", 42),
            new Person("赵六", 42),
            new Person("田七", 28)
    );

    @Test
    public void test01() {
        //从流中排除某些元素
        personList.stream().filter(p -> p.getAge() > 28).forEach(System.out::println);

        System.out.println("-------------------------------------------------");

        //筛选，根据所生成的元素的hashcode()和equals()去除重复元素
        List<Integer> asList = Arrays.asList(1, 2, 3, 4, 1, 2);
        asList.stream().distinct().forEach(System.out::println);

        System.out.println("-------------------------------------------------");

        //limit指定流个数
        List<Integer> asList1 = Arrays.asList(1, 2, 3, 4, 1, 2);
        asList1.stream().limit(3).forEach(System.out::println);

        System.out.println("-------------------------------------------------");

        //skip跳过流中的元素
        List<Integer> asList2 = Arrays.asList(1, 2, 3, 4, 1, 2);
        asList2.stream().skip(2).forEach(System.out::println);
    }
}
